﻿using System;
using System.Collections.Generic;
using System.IO;
using Lidgren.Network;
using Model.LiveControl;

namespace Network.Messages.FileTranser
{
    public class NotifyNewFileCanCopyMessge : NovaMessage
    {
        public NotifyNewFileCanCopyMessge() :
             base((ushort)CustomMessageType.NotifyNewFileCanCopyMessge)
        {
        }

        public void Init(List<string> dirList, List<string> fileList)
        {
            this.DirList = dirList;
            this.FileList = fileList;
            FlieItemList.Clear();
            foreach (var fileName in fileList)
            {
                FileInfo tInfo = new FileInfo(fileName);
                if (tInfo.Exists)
                {
                     AddFileItem(tInfo,"");
                }
            }
            foreach (var dirName in DirList)
            {
                DirectoryInfo tInfo = new DirectoryInfo(dirName);
                if (tInfo.Exists)
                {
                    AddDirItems(tInfo,"");
                }
            }
        }

        private void AddDirItems(DirectoryInfo tInfo, string prifixDir)
        {
            var tItem = new FileItem
            {
                SourceFileName = tInfo.FullName,
                WriteTime = tInfo.LastWriteTime,
                IsFolder = true,
                FileName = prifixDir + tInfo.Name
            };
            FlieItemList.Add(tItem);

            prifixDir = $"{prifixDir}{tInfo.Name}\\";
            foreach (var directoryInfo in tInfo.GetDirectories())
            {
                AddDirItems(directoryInfo, prifixDir);
            }
            foreach (var fileInfo in tInfo.GetFiles())
            {
                AddFileItem(fileInfo, prifixDir);

            }
        }

        private void AddFileItem(FileInfo pInfo, string prifixDir)
        {
            var tItem = new FileItem
            {
                IsFolder = false,
                SourceFileName = pInfo.FullName,
                FileName = prifixDir + pInfo.Name,
                WriteTime = pInfo.LastWriteTime,
                FileSize = pInfo.Length
            };

            FlieItemList.Add(tItem);
        }

        public List<string> DirList = new List<string>();
        public List<string> FileList = new List<string>();
        public List<FileItem>  FlieItemList = new List<FileItem>();
        public override void WritePayload(NetOutgoingMessage message)
        {
            base.WritePayload(message);
            message.Write(DirList.Count);
            foreach (var dir in DirList)
            {
                message.Write(dir);
            }
            message.Write(FileList.Count);
            foreach (var file in FileList)
            {
                message.Write(file);
            }

            message.Write(FlieItemList.Count);
            foreach (var fileItem in FlieItemList)
            {
                fileItem.WritePayload(message);
            }
        }

        public override void ReadPayload(NetIncomingMessage message)
        {
            base.ReadPayload(message);
            DirList.Clear();
            FileList.Clear();
            int dirCount = message.ReadInt32();
            for (int i = 0; i < dirCount; i++)
            {
                DirList.Add(message.ReadString());
            }
            int fileCount = message.ReadInt32();
            for (int i = 0; i < fileCount; i++)
            {
                FileList.Add(message.ReadString());
            }

            int fileItemCount =  message.ReadInt32();
            for (int i = 0; i < fileItemCount; i++)
            {
                var item = new FileItem();
                item.ReadPayload(message);
                FlieItemList.Add(item);
            }

        }
    }
}
